package com.ruanjian2001.controller;

import com.ruanjian2001.Dao.UserDao;
import com.ruanjian2001.entity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 注册新用户
 */
public class RegisterServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String gender = request.getParameter("gender");
        String email = request.getParameter("email");
        String telephone = request.getParameter("telephone");
        String introduce = request.getParameter("introduce");

        //将数据添加到对象
        User us = new User();
        us.setUsername(username);
        us.setPassword(password);
        us.setGender(gender);
        us.setEmail(email);
        us.setTelephone(telephone);
        us.setIntroduce(introduce);
        System.out.println(us);

        //查看数据库中是否存在该用户
         UserDao ud = new UserDao();

        //如果没有搜索到用户返回false
         Boolean bool = ud.findUser(us.getUsername());
         //如果不存在用户，将数据存到数据库，同时跳转到成功页面
         if(bool == false){
             ud.addUser(us);
             response.sendRedirect(request.getContextPath() + "/client/registersuccess.jsp");
         }else{
             response.sendRedirect(request.getContextPath()+"/client/registererror.jsp");
         }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
